﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using DataModel.Managers;
using DataModel.Models;
using UniversityPortal.Controllers.Requests;
using UniversityPortal.Models;
using System.Collections.Generic;

namespace UniversityPortal.Controllers
{
    public class JournalController : Controller
    {
        //
        // GET: /Journal/
        //[UAuthorize(UserRoles = "Student,Admin,Professor")]
        public ActionResult GroupList()
        {
            var gm = new GroupManager();
            return View(gm.GetAllGroups().ToList());
        }

        //[UAuthorize(UserRoles="Student,Admin,Professor")]
        public ActionResult GetGroupJournal(int group, string subject, string from, string to, string subj)
        {
            var jManager = new JournalManager();
            var gManager = new GroupManager();

            if (gManager.GetAllGroups().Count(g => g.Number == group) < 1)
            {
                ViewBag.groupExist = false;
                return View();
            }
            ViewBag.groupExist = true;

            DateTime fromDate;
            DateTime toDate;
            if (!(DateTime.TryParse(from, out fromDate) && DateTime.TryParse(to, out toDate)))
            {
                fromDate = DateTime.Today;
                toDate = DateTime.Today.AddDays(61);
            }

            var subjectList = gManager.GetAllGroupSubjects(group,8);
            SubjectModel selectedSubject = subjectList.FirstOrDefault(s => s.ShortName == subj);
            JournalModel journal = null;
            if (selectedSubject != null)
            {
                journal = jManager.GetJournalByGroup(group, selectedSubject, fromDate, toDate);
            }
            else if (subjectList.Count > 0)
            {
                journal = jManager.GetJournalByGroup(group, subjectList[0], fromDate, toDate);
            }
            
            
            ViewBag.journalExist = journal != null;


            ViewBag.subjectList = subjectList;
            return View(journal);
        }

        [HttpPost]
        //[UAuthorize(UserRoles = "Professor")]
        public void SetMark(string jsonData)
        {
            var manager = new JournalManager();
            var serializer = new JavaScriptSerializer();

            var markRequest = serializer.Deserialize<MarkRequest>(jsonData);
            if (markRequest.mark == -1)
            {
                manager.SetMark(markRequest.studentid, markRequest.subjectid, new DateTime(markRequest.date), null);
            }
            else
            {
                manager.SetMark(markRequest.studentid, markRequest.subjectid, new DateTime(markRequest.date), markRequest.mark);
            }
        }

        [HttpPost]
        //[UAuthorize(UserRoles = "Professor")]
        public void SetPresence(string jsonData)
        {
            var manager = new JournalManager();
            var serializer = new JavaScriptSerializer();

            var presenceRequest = serializer.Deserialize<PresenceRequest>(jsonData);
            if (presenceRequest.presence == -1)
            {
                manager.SetPresence(presenceRequest.studentid, presenceRequest.subjectid, new DateTime(presenceRequest.date), null);
            }
            else
            {
                manager.SetPresence(presenceRequest.studentid, presenceRequest.subjectid, new DateTime(presenceRequest.date), presenceRequest.presence);
            }
        }
    }
}
